package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxBridgeEntity;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.entity.CbVulnerabilityEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.BridgeParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.CbVulnerabilityRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class CbVulnerabilityRepositoryImpl implements CbVulnerabilityRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void updateVulnerability(CbVulnerabilityEntity it) {
        String sql = "update czt_bridge_vulnerability set earthquake_intensity= :earthquakeIntensity,site_soil = :siteSoil,foundation_fail= :foundationFail,super_structure= :superStructure,bearing= :bearing,pier_height= :pierHeight," +
                "pier_material= :pierMaterial,basement= :basement,length= :length,w0= :w0,comment_info= :commentInfo,update_user= :updateUser,update_time =:updateTime,bridge_name= :bridgeName,years= :years," +
                "fortification= :fortification,user_name= :userName,seismic_damage_grade= :seismicDamageGrade,ai= :ai,ai_correct= :aiCorrect,seismic_damage_grade_correct= :seismicDamageGradeCorrect" +
                " where cbv_id= '" + it.getCbvId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public boolean deleteVulnerability(String id) {
        String sql = "update czt_bridge_vulnerability set del_flag = '1' where cbv_id=?";
        return jdbcTemplate.update(sql, id) > 0;
    }

    @Override
    public CbVulnerabilityEntity getVulnerabilityById(String id) {
        String sql = "select * from czt_bridge_vulnerability where del_flag='0' and cbv_id='" + id + "' order by create_time asc";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(CbVulnerabilityEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public boolean saveBridgePoint(SmxBridgeEntity it) {
        String sql = "insert into czt_smx_bridge_point values (:id,:bridgeId,:longitude,:latitude,:acrossObject,:intensity,:year,:fortification,:structure,:maxSpan,:length,:siteSoil,:grade,:foundationFail,:superStructure,:bearing,:pierHeight,:pierMaterial,:basement,:number,:capacity,:abutmentMat,:abutmentType,:expansionJoint,:width,:note,ST_GeometryFromText(:geom,4490),:versionId,:tableId,:delFlag,:name,:calculationResults,:calculationResultsCorrect,:damage,:damageCorrect,:serialNumber,:createUser,:createTime,:updateUser,:updateTime,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public boolean saveBridgeLine(SmxBridgeEntity it) {
        String sql = "insert into czt_smx_bridge_line values (:id,:bridgeId,:longitude,:latitude,:acrossObject,:intensity,:year,:fortification,:structure,:maxSpan,:length,:siteSoil,:grade,:foundationFail,:superStructure,:bearing,:pierHeight,:pierMaterial,:basement,:number,:capacity,:abutmentMat,:abutmentType,:expansionJoint,:width,:note,ST_GeometryFromText(:geom,4490),:versionId,:tableId,:delFlag,:name,:calculationResults,:calculationResultsCorrect,:damage,:damageCorrect,:serialNumber,:createUser,:createTime,:updateUser,:updateTime,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public List<SmxBridgeEntity> getByVersionId(String versionId) {
        try {
            String sql = "select * from czt_smx_bridge_line where version_id = '" + versionId + "' union all select * from czt_smx_bridge_point where version_id = '" + versionId + "'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmxBridgeEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveBridgeResult(SmxBridgeEntity entity, Integer bridgeType) {
        String tableName = "czt_smx_bridge_point";
        if (bridgeType == 1) {
            tableName = "czt_smx_bridge_line";
        }
        jdbcTemplate.update("update "+tableName+" set calculation_results=?,calculation_results_correct=?,damage=?,damage_correct=? where id = ?",
                entity.getCalculationResults(), entity.getCalculationResultsCorrect(), entity.getDamage(), entity.getDamageCorrect(), entity.getId());
    }

    @Override
    public SmxBridgeEntity getByTableId(String id, Integer bridgeType) {
        try {
            String tableName = "czt_smx_bridge_point";
            if (bridgeType == 1) {
                tableName = "czt_smx_bridge_line";
            }
            String sql = "select * from " + tableName + " where table_id = '" + id + "'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SmxBridgeEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }
    @Override
    public void updateBridgeById(BridgeParam entity, Integer bridgeType) {
        String tableName = "czt_smx_bridge_point";
        if (bridgeType == 0) {
            tableName = "czt_smx_bridge_line";
        }
        jdbcTemplate.update("update " + tableName + " set name=?,intensity=?,site_soil=?,foundation_fail=?,super_structure=?,bearing=?,pier_height=?,pier_material=?,basement=?,number=?,note=?,calculation_results_correct=?,damage=?,damage_correct=? where id=?",
                entity.getName(), entity.getIntensity(), entity.getSiteSoil(), entity.getFoundationFail(), entity.getSuperStructure(), entity.getBearing(), entity.getPierHeight(), entity.getPierMaterial(), entity.getBasement(), entity.getNumber(), entity.getNote(), entity.getCalculationResultsCorrect(), entity.getDamage(), entity.getDamageCorrect(), entity.getId());
    }

    @Override
    public DestructiveStateVo getByBasicInfo(SmxBridgeEntity entity,String year) {
        try {
            StringBuilder sql = new StringBuilder("select * from czt_smx_basic_library_bridge where del_flag = '0' and structural_style = '" + entity.getSuperStructure() + "' and site_classification = '" + entity.getSiteSoil() + "' and year = '" + year + "' and fortification = '" + entity.getFortification() + "' and range =" + 0);
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DestructiveStateVo.class));
        } catch (DataAccessException e) {
            return new DestructiveStateVo();
        }
    }
}
